/// ===========================================================================
/// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
/// KIND, WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
/// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
/// PURPOSE.
/// ===========================================================================
/// 
/// Project:        MOSS Faceted Search
/// Author:         Leonid Lyublinski (leonidly@microsoft.com)
/// Company:        Microsoft Services
/// Date:           07/04/2008  Version:        2.5
///
/// ===========================================================================

using System;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
using System.Xml.Serialization;
using System.Data;
using Microsoft.Office.Server;
using Microsoft.Office.Server.Search.Query;
using EntLib = Microsoft.Practices.EnterpriseLibrary;
using Microsoft.SharePoint.Portal.ExtendedSearch.Common;

namespace Microsoft.SharePoint.Portal.ExtendedSearch.WebControls
{

    /// <summary>
    /// Class offers support to Faceted Search web parts that need to extract data from query string or HTTP POST request
    /// </summary>
    public class SearchQueryWebPart : WebPart
    {
        private SearchQuery _query;
        private string key = string.Format("{0}_{1}", Constants.KEY, "SearchQuery");

        /// <summary>
        /// Key property that rerurns request data 
        /// </summary>
        /// <remarks>Will use Clone to prevent changes done on SearchQuery</remarks>
        internal SearchQuery Query
        {
            get
            {
                if (_query.IsEmpty())
                {
                    _query = Utility.ExtractQuery();
                }
                ViewState["SearchQuery"] = _query.GetXml().OuterXml;
                return _query.Clone();                
            }
            set
            {
                // TESTING IN PROGRESS
                _query = value;
                ViewState["SearchQuery"] = _query.GetXml().OuterXml;
            }
        }
        
        private void FeedFormData(SearchQuery query)
        {
            if (!query.IsEmpty())
                query.FormData = new FormData(5, query.GetXml().OuterXml);
        }
    }
}